﻿using System.Collections.Generic;
using Engine;
using Engine.Graphics;
using Game;
namespace Mekiasm
{
    public class TransmitItem : Item
    {
        public static int Index;
        public Color global = Color.White;
        public List<BoundingBox> boundingBoxes = new List<BoundingBox>();

        public TransmitItem(Color color, string n) : base(n)
        {
            global = color;
            
            BoundingBox bounding = new BoundingBox();
            bounding.Min = Vector3.Zero;
            bounding.Max = Vector3.Zero;
            boundingBoxes.Add(bounding);
        }
        public override string GetDisplayName(SubsystemTerrain subsystemTerrain, int value)
        {
            return DisplayName;
        }
        public override bool IsVisible(int value)
        {
            return false;
        }
        public override int GetFaceTextureSlot(int face, int value)
        {
            return 255;
        }
        public override string GetCategory(int value)
        {
            return MekiasmInit.categories_pipe;
        }
        public override BlockPlacementData GetDigValue(SubsystemTerrain subsystemTerrain, ComponentMiner componentMiner, int value, int toolValue, TerrainRaycastResult raycastResult)
        {
            return new BlockPlacementData() { Value = 0, CellFace = raycastResult.CellFace };
        }
        public override BoundingBox[] GetCustomCollisionBoxes(SubsystemTerrain terrain, MovingBlock value)
        {
            return boundingBoxes.ToArray();
        }
        public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
        {
            ILibrary.DrawCubeBlock(primitivesRenderer, value, new Vector3(size), ref matrix, global, global, environmentData);
        }
        public override void GenerateTerrainVertices(Block block, BlockGeometryGenerator generator, TerrainGeometry geometry, int value, int x, int y, int z)
        {
            generator.GenerateCubeVertices(block, value, x, y, z, global, geometry.TransparentSubsetsByFace);
        }
        public override BoundingBox[] GetCustomInteractionBoxes(SubsystemTerrain terrain, MovingBlock value)
        {
            return new BoundingBox[] { new BoundingBox() };
        }
        public override void OnNeighborBlockChanged(int x, int y, int z, int neighborX, int neighborY, int neighborZ)
        {
            Point3 point3 = Point3.Zero;
            for (int i = y - 2; i < y; i++)
            {
                int v = ILibrary.SubsystemTerrain.Terrain.GetCellValueFast(x, i, z);
                if (ILibrary.GetBasicValue(v) == TransmitMachine.Index) point3 = new Point3(x, i, z);
            }
            bool flag = !TransmitMachine.CheckTransmitDoor(point3);
            if (flag && TransmitMachine.saves.TryGetValue(point3, out MutiBlockCheck.TranmitResult tranmitResult))
            {
                TransmitMachine.saves.Remove(point3);
                ILibrary.RemoveBlockEntity(point3.X, point3.Y, point3.Z);
                if (tranmitResult.Connected && TransmitMachine.saves.TryGetValue(tranmitResult.Connection, out MutiBlockCheck.TranmitResult result))
                {
                    //清除连接的传送门的方块
                    TransmitMachine.SetInsideBlocks(result.insidePoints, 0);
                }
                TransmitMachine.SetInsideBlocks(tranmitResult.insidePoints, 0);
            }
        }
    }
}
